﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

using Jarvis.Analytics;
using Jarvis.Utility;

namespace Jarvis.Windows
{
    /// <summary>
    /// Interaction logic for CorrectPredictionsWindow.xaml
    /// </summary>
    public partial class CorrectPredictionsWindow : Window
    {
        public Jarvis.App app;

        public CorrectPredictionsWindow()
        {
            InitializeComponent();

            this.Refresh();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            app.correctPrediction(activity.Text, location.Text, social.Text);

            this.Hide();
        }

        public void Refresh()
        {
            object act_item = activity.SelectedItem;
            object loc_item = location.SelectedItem;
            object soc_item = social.SelectedItem;

            activity.Items.Clear();
            location.Items.Clear();
            social.Items.Clear();

            List<String> activities = (Preferences.preferences.Get("Activity Labels") as List<String>);
            List<String> locations = (Preferences.preferences.Get("Location Labels") as List<String>);
            List<String> socials = (Preferences.preferences.Get("Social Labels") as List<String>);

            activities.Sort();
            locations.Sort();
            socials.Sort();

            foreach (string s in activities)
                activity.Items.Add(s);

            foreach (string s in locations)
                location.Items.Add(s);

            foreach (string s in socials)
                social.Items.Add(s);

            activity.SelectedItem = act_item;
            location.SelectedItem = loc_item;
            social.SelectedItem = soc_item;
        }

        private void SelectItem(string item, ComboBox box)
        {
            if (item == null || box == null)
                return;

            if (!box.Items.Contains(item))
                box.Items.Add(item);

            foreach (object o in box.Items)
            {
                if (o.Equals(item))
                    box.SelectedItem = o;
            }
        }

        private void UpdateLabel(object o)
        {
            Dictionary<String, String> d = (Dictionary<String, String>)o;

            foreach (String key in d.Keys)
            {
                String prediction = d[key];
             
                if (key.Equals("Activity"))
                    this.SelectItem(prediction, activity);
                else if (key.Equals("Location"))
                    this.SelectItem(prediction, location);
                else if (key.Equals("Social"))
                    this.SelectItem(prediction, social);
            }
        }

        public void PredictionUpdated(object sender, EventArgs ea, String key, String prediction)
        {
            if (!this.IsVisible)
            {
                Dictionary<String, String> d = new Dictionary<String, String>();
                d[key] = prediction;

                base.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                                            new System.Threading.WaitCallback(UpdateLabel), d);
            }
        }

        private void Window_Closing(object sender, CancelEventArgs e)
        {
            Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background,
                (DispatcherOperationCallback)delegate(object o)
                {
                    Hide();
                    return null;
                }, null);

            AnalyticsManager.LogEvent("Window Closing", "Correct Prediction");

            e.Cancel = true;
        }
    }
}
